class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> v;
        int n=nums.size();
        for(int i=0;i<=n-3;++i)
        {
            if(i>0&&nums[i]==nums[i-1])
                continue;
            int left=i+1;
            int right=n-1;
            int t=nums[i];
            while(left<right)
            {
                if(t+nums[left]+nums[right]>0)
                {
                    right--;
                }
                else if(t+nums[left]+nums[right]<0)
                {
                    left++;
                }
                else
                {
                    v.push_back({nums[i],nums[left],nums[right]});
                    while(left<right&&nums[left]==nums[left+1])
                        left++;
                    while(left<right&&nums[right]==nums[right-1])
                        right--;
                    right--;
                    left++;
                }
            }
        }
        return v;
    }
};
